
<?
$id = Zend_Controller_Front::getInstance()->getRequest()->getParam('id');

$form = $this->element;

foreach ($form->getElements() as $e) {
	if ($e instanceof Zend_Form_Element_Checkbox) {
		$label = $e->getDecorator('Label');
		$label->setOption('placement', Zend_Form_Decorator_Abstract::APPEND);
		$label->setTag('dd');
		
		$tag = $e->getDecorator('HtmlTag');
		$tag->setTag('dt');
		//$e->removeDecorator('Label');
		//$e->addDecorator($label);
		
		//echo '<pre>';
		//var_dump($e->getDecorators());die();
	}
}

?>
<div class="hidden_filter" style="display:none">	
	<a href="#" id="del_filter">X</a>
		<select class="filter_field" >
<?			
			$spamFilterFieldsRows =SpamFilterFieldsPeer::getInstance()->fetchAll(); 
			foreach($spamFilterFieldsRows as $FieldsRow){
				echo '<option value="'.$FieldsRow->getId().'" type_field="'.$FieldsRow->getType().'">'.$FieldsRow->getName().'</option>';
			}			
?>
	</select>
		<select class="filter_ratio">
<?			
			$spamFilterRatioRows =SpamFilterRatioPeer::getInstance()->fetchAll(); 
			foreach($spamFilterRatioRows as $RatioRow){
				echo '<option value="'.$RatioRow->getId().'">'.$RatioRow->getName().'</option>';
			}			
?>
	</select>
		
		
		<input class="filter_value" type="text">
		<input class="filter_general" name="filter_general" type="hidden">	
		
	</div>

<link rel="stylesheet" media="screen" href="/style/admin_product.css" />
<script type="text/javascript" src="/javascript/admin_product.js"></script>
<!--[if lte IE 7]><link href="/style/ie.css" rel="stylesheet" type="text/css"><![endif]-->
<form enctype="multipart/form-data" method="post" action="">
  <input id="form_id" type="hidden" value="<? echo $id; ?>">
  <input id="qqq" type="hidden" value="1">
  <input id="qqq" type="hidden" value="2">
  <div>
    
    <?php 
    /**
     * Translate some form errrors.
     * 
     * @param string $key
     * @param string $error
     * @return string
     */
    function _translate($key, $error)
    {
    	$_translated = array(
			'isEmpty' => 'Поле обязательно для заполнения',
		);
	
    	if (isset($_translated[$key])) {
    		return $_translated[$key];
    	}
    	
    	return $error;
    }
    
    $errors = $form->getMessages();
    foreach ($errors as $key => $error) {
    	if (is_array($error)) {
    		unset($errors[$key]);
    	}

   		if (is_array($error)) {
     		foreach ($error as $k => $v) {
     			$label = $form->getElement($key)->getLabel();
     			$v = _translate($k, $v);
     			$errors[$key][$k] = "$label -- $v";
     		}
    	} else {
    		$errors[$key] = _translate($key, $error);
    	}
    }
    
    if ($errors) {
	    echo "<ul class='errors'>";
	    foreach ($errors as $error) {
	    	if (is_array($error)) {
	    		foreach ($error as $k => $v) {
	    			echo "<li>$v</li>";
	    		}
	    	} else {
	    		echo "<li>$error</li>";
	    	}
	    }
	    echo "</ul>";
    }
    ?>
    
    <div id="filters">
	

		</div>
<input type="button" id="add_filter" value="Добавить условие">
    
    
    
    <fieldset>
      <legend>Рассылка</legend>
      В тексте используйте теги: <b>[NAME], [CHILD_NAME], [CHILD_AGE], [LAST_ORDER_DATE]</b>
      <dl class="floater-5">
       	<?php echo $form->getElement('theme'); ?>
      </dl>
      <dl class="floater-51">
        <?php 
        $form->getElement('text')->setAttrib("class", "");
        echo $form->getElement('text'); ?>
      </dl>
	 	 <dl class="floater-51">	 	 		
  			<?php echo $form->getElement('file_background'); ?>    
      </dl>
	 	 <dl class="floater-51">
  			<?php echo $form->getElement('dt'); ?>    
      </dl>
       <dl class="floater-51">       	
  			<?php echo $form->getElement('is_active'); ?>    
      </dl>

   
  </div>
  <input type="hidden" name="filters_all" id="filters_all" value="">
  <input type="button" name="copy" id="copy" value="Копировать рассылку">
  
  <dl class="floater-51">
	   <dd id="submit-element">
	        <input type="submit" name="submit" id="submit" value="Сохранить">
		   <?php echo $form->getElement('crud_exit'); ?>
	   </dd>
 	</dl>
</form>

<script>
	function loadfilters(){
		
		var id = $("#form_id").val();
		
		if(id > 0){
			$.post('/newsletters/getlistfilters/', {id:id}, function(data) {
					var str_filter = $(".hidden_filter").html();	
					
					for(var i=0;i<data.arr.length;i++){
						
						str = '<div class="filter" id="f'+i+'">'+str_filter +'</div>';
						$("#filters").append(str);
						
						$("#f"+i+" .filter_field").find('option[value='+data.arr[i].field_id+']').attr('selected', true);
						$("#f"+i+" .filter_ratio").find('option[value='+data.arr[i].ratio_id+']').attr('selected', true);
						if(data.arr[i].field_type=="txt" || data.arr[i].field_type=="dt")
							$("#f"+i+" .filter_value").val(data.arr[i].value);
						if(data.arr[i].field_type=="list_gender"){
							$("#f"+i+" .filter_value").remove();
							str='<select class="filter_value"><option value=""></option><option value=2>Мужской</option><option value=3>Женский</option></select>';
						  $("#f"+i).append(str);
						  $("#f"+i+" .filter_value").find('option[value='+data.arr[i].value+']').attr('selected', true);						
								 			 
						}
						filter_val=$("#f"+i+" .filter_field").val()+'||'+
								 			 $("#f"+i+" .filter_ratio").val()+'||'+
								 			 $("#f"+i+" .filter_value").val()+'||';
						$("#f"+i+" .filter_general").val(filter_val);
						
					}
				}, 'json');
		}
	}
	var str;

	$(function() {
		var id = $("#form_id").val();
		loadfilters();
		
		$("#copy").click(function() {	
			if(id > 0){
				$.post('/newsletters/copyrecord/', {id:id}, function(data) {
					alert("Создана новая рассылка ID="+data.new_id);
				}, 'json');
			}else
				alert("Cохраните рассылку!");
		});
		
		
		$("#add_filter").click(function() {	
			var str_filter = $(".hidden_filter").html();								
			str = '<div class="filter" name="">'+str_filter +'</div>';
			$("#filters").append(str);			
		});
				
		$("#submit").click(function() {	
				var str="";
				$(".filter_general").each(function() {					
					str+= $(this).val();
					if($(this).val().length >0) {
						str+="::";}
				});
				$("#filters_all").val(str);
		});
		
		
		$(".filter_ratio, .filter_value").live('change',function() {	
		
			filter_val=$(this).parents('.filter').find('.filter_field').val()+'||'+
								 $(this).parents('.filter').find('.filter_ratio').val()+'||'+
								 $(this).parents('.filter').find('.filter_value').val()+'||';
			if($(this).parents('.filter').find('.filter_value').val().length >0)
				$(this).parents('.filter').find('.filter_general').val(filter_val);	
			else	
				$(this).parents('.filter').find('.filter_general').val("");	
		});
		
		$(".filter_field").live('change',function() {
					
				if($(this).find('option:selected').attr('type_field')=="list_gender"){
					$(this).parents('.filter').find('.filter_value').remove();
					str='<select class="filter_value"><option value=""></option><option value=2>Мужской</option><option value=3>Женский</option></select>';
					$(this).parents('.filter').append(str);
				}
				if($(this).find('option:selected').attr('type_field')=="txt"){
					$(this).parents('.filter').find('.filter_value').remove();
					str='<input class="filter_value" type="text">';
					$(this).parents('.filter').append(str);
				}
				if($(this).find('option:selected').attr('type_field')=="dt"){
					$(this).parents('.filter').find('.filter_value').remove();
					str='<input class="filter_value date hasDatepicker" type="text">';
					$(this).parents('.filter').append(str);
				}
		});	
		
		$("#del_filter").live('click',function() {
				$(this).parents('.filter').remove();
			});
		

	});
</script>

